<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Netty梳理 | yili的个人小站</title><meta name="keywords" content="Netty"><meta name="author" content="yili"><meta name="copyright" content="yili"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="NettyNetty在java中使用非常广泛，通过对Java NIO进行梳理得到的一个易于使用的IO类型框架。  1.引导存在两种类型的引导方式：一种用于客户端的是Bootstrap，另一种是用于服务端的ServerBootstrap。  ServerBootstrap存在两个EventLoopGroup，也就是在绑定这个引导时需要建立两个group，他们两个分别有着不同的作用，一个是监听作用，另">
<meta property="og:type" content="article">
<meta property="og:title" content="Netty梳理">
<meta property="og:url" content="https://yili979.gitee.io/blog/2021/10/04/java/Netty%E6%A2%B3%E7%90%86/index.html">
<meta property="og:site_name" content="yili的个人小站">
<meta property="og:description" content="NettyNetty在java中使用非常广泛，通过对Java NIO进行梳理得到的一个易于使用的IO类型框架。  1.引导存在两种类型的引导方式：一种用于客户端的是Bootstrap，另一种是用于服务端的ServerBootstrap。  ServerBootstrap存在两个EventLoopGroup，也就是在绑定这个引导时需要建立两个group，他们两个分别有着不同的作用，一个是监听作用，另">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/10/04/O719aYEAueVQTsn.png">
<meta property="article:published_time" content="2021-10-04T11:06:01.000Z">
<meta property="article:modified_time" content="2021-10-04T15:51:41.179Z">
<meta property="article:author" content="yili">
<meta property="article:tag" content="Netty">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/10/04/O719aYEAueVQTsn.png"><link rel="shortcut icon" href="/blog/img/favicon.ico"><link rel="canonical" href="https://yili979.gitee.io/blog/2021/10/04/java/Netty%E6%A2%B3%E7%90%86/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Netty梳理',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-10-04 23:51:41'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/blog/css/custom.css" media="defer" onload="this.media='all'"><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/10/05/BKpC6sFEo5Dl7qA.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">28</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">20</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">13</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://i.loli.net/2021/10/04/O719aYEAueVQTsn.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">yili的个人小站</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Netty梳理</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-10-04T11:06:01.000Z" title="发表于 2021-10-04 19:06:01">2021-10-04</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-10-04T15:51:41.179Z" title="更新于 2021-10-04 23:51:41">2021-10-04</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/Java-NIO/">Java NIO</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.8k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>5分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Netty梳理"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Netty"><a href="#Netty" class="headerlink" title="Netty"></a>Netty</h1><p>Netty在java中使用非常广泛，通过对Java NIO进行梳理得到的一个易于使用的IO类型框架。</p>
<p><img src="https://i.loli.net/2021/10/04/eW4uQRdOSz5NnGc.png"></p>
<h2 id="1-引导"><a href="#1-引导" class="headerlink" title="1.引导"></a>1.引导</h2><p>存在两种类型的引导方式：一种用于客户端的是Bootstrap，另一种是用于服务端的ServerBootstrap。</p>
<p><img src="https://i.loli.net/2021/10/04/dqHcIUPWlnTQv8e.png" alt="image-20210917114716819"></p>
<p>ServerBootstrap存在两个EventLoopGroup，也就是在绑定这个引导时需要建立两个group，他们两个分别有着不同的作用，一个是监听作用，另一个是接受来自客户端的Channel。</p>
<h2 id="2-ByteBuf"><a href="#2-ByteBuf" class="headerlink" title="2.ByteBuf"></a>2.ByteBuf</h2><p><strong>优点：</strong></p>
<ul>
<li>可以被用户自定义的缓冲区类型扩展</li>
<li>通过内置的复合缓冲区类型实现了透明的零拷贝</li>
<li>容量可以按需增长</li>
<li>在读和写这两种模式之间切换不需要调用ByteBuffer的flip方法</li>
<li>读和写使用了不同的索引</li>
<li>支持方法的链式调用</li>
<li>支持引用计数</li>
<li>支持池化</li>
</ul>
<h3 id="ByteBuf-主要分为三种类型"><a href="#ByteBuf-主要分为三种类型" class="headerlink" title="ByteBuf 主要分为三种类型"></a>ByteBuf 主要分为三种类型</h3><ul>
<li><p>Pooled 和 Unpooled，池化和非池化；</p>
</li>
<li><p>Heap 和 Direct，堆内存和直接内存；</p>
</li>
<li><p>Safe 和 Unsafe，安全和非安全。</p>
</li>
</ul>
<p><strong>池化和非池化</strong>： 池化就是用完就放回池子里面，比如我们所熟悉的数据库连接池。非池化就是每次使用都重新创建，使用完成则立马销毁。从性能的角度来说，池化会比非池化相对高，因为可以重复利用，避免每次都重新创建。</p>
<p><strong>堆内存和直接内存</strong>： 堆内存是 JVM 内部开辟的一块内存空间，它的生命周期受到 JVM 来管理，不容易造成内存溢出的情况。直接内存则是直接受操作系统管理了，如果数据量很大的情况，容易造成内存溢出情况。</p>
<p><strong>安全和非安全</strong>： 主要是 Java 操作底层操作数据的一种安全和非安全的方式。</p>
<p>根据不同类型进行组合，得到常见 ByteBuf 的实现类</p>
<p>池化 + 堆内存，PooledHeapByteBuf；<br>池化 + 直接内存，PooledDirectByteBuf；<br>池化 + 堆内存 + 不安全，PooledUnsafeHeapByteBuf；<br>池化 + 直接内存 + 不安全，PooledUnsafeDirectByteBuf；<br>非池化 + 堆内存，UnpooledHeapByteBuf；<br>非池化 + 直接内存，UnpooledDirectByteBuf；<br>非池化 + 堆内存 + 不安全，UnpooledUnsafeHeapByteBuf；<br>非池化 + 直接内存 + 不安全，UnpooledUnsafeDirectByteBuf</p>
<h3 id="2-1ByteBuf的使用模式"><a href="#2-1ByteBuf的使用模式" class="headerlink" title="2.1ByteBuf的使用模式"></a>2.1ByteBuf的使用模式</h3><p>1.堆缓冲区</p>
<p> 堆缓冲区就是将数据存储在JVM的空间中。模式被称为支撑数组（backing array），可以在没有池化的情况下提供快速的分配和释放。</p>
<ul>
<li>优点：可以看见，对堆缓冲区中的数据进行访问的时候，直接返回这个缓冲区数组的引用。</li>
<li>缺点：在每次进行IO操作的时候都需要将数据复制到直接缓冲区中</li>
</ul>
<p><img src="https://i.loli.net/2021/10/04/w7ukEUcMRSyAG6q.png" alt="image-20210917153123984"></p>
<h4 id="2-直接缓冲区"><a href="#2-直接缓冲区" class="headerlink" title="2.直接缓冲区"></a>2.直接缓冲区</h4><p>直接缓冲区的内容是驻留在堆缓冲区之外的，在IO操作时可以直接将数据发送出去。</p>
<p>它的缺点和优点和堆缓冲区正好相反</p>
<p>每次对直接缓冲区中的数据进行访问时，不能直接返回一个数组的引用，而是要将复制到一个新生成的数组中，通过这个新数组来进行访问。</p>
<p><img src="https://i.loli.net/2021/10/04/mbhBUTEjwiMz2sf.png" alt="image-20210917154306028"></p>
<h4 id="3-复合缓冲区"><a href="#3-复合缓冲区" class="headerlink" title="3.复合缓冲区"></a>3.复合缓冲区</h4><p>复合缓冲区就是基于上述的两种缓冲区类型来进行实现的。</p>
<p>Composite是Netty所特有的一个缓冲区模式，可以复合地使用两种缓冲区模式</p>
<p>通过建立一个复合的缓冲模式，然后将另外的两个种类放入到这个组件中</p>
<p><img src="https://i.loli.net/2021/10/04/dsQVIxqbL1EOcg4.png" alt="image-20210917154738274"></p>
<h3 id="2-2字节级操作"><a href="#2-2字节级操作" class="headerlink" title="2.2字节级操作"></a>2.2字节级操作</h3><h4 id="2-2-1随机读写"><a href="#2-2-1随机读写" class="headerlink" title="2.2.1随机读写"></a>2.2.1随机读写</h4><p>通过调用get方法进行随机读写</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">ByteBuf byteBuf = Unpooled.copiedBuffer(<span class="string">&quot;yili&quot;</span>, CharsetUtil.UTF_8);</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; byteBuf.capacity(); i++) &#123;</span><br><span class="line">    <span class="keyword">byte</span> b = byteBuf.getByte(i);</span><br><span class="line">    System.out.println((<span class="keyword">char</span>) b);</span><br><span class="line">&#125;</span><br><span class="line">System.out.println(byteBuf.toString(CharsetUtil.UTF_8));</span><br></pre></td></tr></table></figure>

<h4 id="2-2-2两种索引"><a href="#2-2-2两种索引" class="headerlink" title="2.2.2两种索引"></a>2.2.2两种索引</h4><p>由于ByteBuf中存在两种索引，所以在进行读写的时候不需要使用flip来进行读写转换，不用对此时是读模式还是写模式来进行判断。</p>
<p><img src="https://i.loli.net/2021/10/04/ziM6CtFr7o2ehsj.png" alt="image-20210917173038912"></p>
<p>可以在图中看见两个索引操作模式，readerIndex之前的字节都是可以进行抛弃的字节，中间的字节为可读字节，最后的是没有写入的字节，为可写字节。</p>
<h2 id="3-Channel"><a href="#3-Channel" class="headerlink" title="3.Channel"></a>3.Channel</h2><p>在netty中也存在一个Channel，和 Java 原生 NIO 中的 Channel </p>
<p><img src="https://i.loli.net/2021/10/04/Gt3kCwQhLA51jyH.png" alt="在这里插入图片描述"></p>
<p>在Channel中存在着三个组件：ChannelPipeline,ChannelContextHandler,ChannelHandler.</p>
<p>这三个组件之间的关系如下：</p>
<p><img src="https://i.loli.net/2021/10/04/VYunZLcR4jJGHBA.png" alt="图片描述"></p>
<p>pipline的使用很单一，一般就是将一个Handler加入到这个管道中。</p>
<h3 id="ChannelHandler"><a href="#ChannelHandler" class="headerlink" title="ChannelHandler"></a>ChannelHandler</h3><p>ChannelHandler也包含有两种传递方式：第一种是接入消息的传递，另一种是发出消息的传递</p>
<p><strong>Inbound 的顺序</strong></p>
<p>流转顺序： 多个 Inbound 不会自动往下流转，需要手工调用 ctx.fireChannelRead(msg); 才能流转到下一个；</p>
<p>执行顺序： 业务逻辑的执行顺序，则根据 ctx.fireChannelRead(msg); 和逻辑的先后顺序所决定；</p>
<p>Inbound 往 Outbound 流转，则需要手工 ctx.channel().writeAndFlush()。<br><strong>Outbound 的顺序</strong></p>
<p>流转顺序： 多个 Outbound 不会自动往下流转，需要手工调用 ctx.write(msg, promise); 才能流转到下一个；</p>
<p>执行顺序： 业务逻辑的执行顺序，则根据 ctx.write(msg, promise); 和逻辑的先后顺序所决定。</p>
<p>在Handler中生命周期如下：</p>
<p><img src="https://i.loli.net/2021/10/04/LjgVnUq5BDGolCb.png" alt="图片描述"></p>
<p>需要注意的问题：</p>
<ol>
<li>注意 channelRead 和 channelReadComplete 之间的关系。</li>
<li>注意 channel关闭会发生的事情</li>
<li>注意在不同阶段所执行的次数</li>
</ol>
<h3 id="EventLoop"><a href="#EventLoop" class="headerlink" title="EventLoop"></a>EventLoop</h3><p>EventLoop的核心作用是，将连接进入的客户端分配一个Channel，并且给Channel分配一个EventLoop和ChannelPipeline。一个负责Channel相关的业务处理，另一个负责管理业务逻辑。</p>
<ol>
<li>监控端口</li>
<li>处理端口事件，将其分发</li>
<li>处理队列事件</li>
</ol>
<p>EvenLoop和Channel是一对多的关系</p>
<p><img src="https://i.loli.net/2021/10/04/LBWgycwKEST41oC.png" alt="image-20210918201414993"></p>
<p>EventLoopGroup 负责为每个新创建的 Channel 分配一个 EventLoop。在当前实现中， 使用顺序循环（round-robin）的方式进行分配以获取一个均衡的分布，并且相同的 EventLoop 可能会被分配给多个 Channel。（这一点在将来的版本中可能会改变。） </p>
<p>一旦一个 Channel 被分配给一个 EventLoop，它将在它的整个生命周期中都使用这个 EventLoop（以及相关联的 Thread）。请牢记这一点，因为它可以使你从担忧你的 ChannelHandler 实现中的线程安全和同步问题中解脱出来。 </p>
<p>另外，需要注意的是，EventLoop 的分配方式对 ThreadLocal 的使用的影响。因为一个 EventLoop 通常会被用于支撑多个 Channel，所以对于所有相关联的 Channel 来说， ThreadLocal 都将是一样的。这使得它对于实现状态追踪等功能来说是个糟糕的选择。然而， 在一些无状态的上下文中，它仍然可以被用于在多个 Channel 之间共享一些重度的或者代价昂 贵的对象，甚至是事件。</p>
<blockquote>
<p>但是在阻塞模型下，数据的传播是基于一个Channel绑定一个EventLoop，不支持绑定多个。</p>
</blockquote>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">yili</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://yili979.gitee.io/blog/2021/10/04/java/Netty%E6%A2%B3%E7%90%86/">https://yili979.gitee.io/blog/2021/10/04/java/Netty%E6%A2%B3%E7%90%86/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://yili979.gitee.io/blog" target="_blank">yili的个人小站</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/Netty/">Netty</a></div><div class="post_share"><div class="social-share" data-image="https://i.loli.net/2021/10/04/O719aYEAueVQTsn.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2021/10/04/java/NIO/"><img class="prev-cover" src="https://i.loli.net/2021/10/04/juhE1Cgk82ZpQHX.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">NIO</div></div></a></div><div class="next-post pull-right"><a href="/blog/2021/10/04/pattern/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F-%E8%A1%8C%E4%B8%BA%E5%9E%8B%E6%A8%A1%E5%BC%8F/"><img class="next-cover" src="https://i.loli.net/2021/10/04/HbJYwl6DFys9kcx.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">设计模式-行为型模式</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/10/05/BKpC6sFEo5Dl7qA.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">yili</div><div class="author-info__description">身骑白马</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">28</div></a></div><div class="card-info-data-item is-center"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">20</div></a></div><div class="card-info-data-item is-center"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">13</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/qiuyuyin"><i class="fab fa-github"></i><span>github🍟</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">啦啦啦，写的不好还请见谅😉<div class="twopeople"><div class="twopeople"><div class="container"style="height:200px;"><canvas class="illo"width="800"height="800"style="max-width: 200px; max-height: 200px; touch-action: none; width: 640px; height: 640px;"></canvas></div><script src="js/twopeople1.js"></script><script src="js/zdog.dist.js"></script><script id="rendered-js"src="js/twopeople.js"></script><style>.twopeople{margin:0;align-items:center;justify-content:center;text-align:center}canvas{display:block;margin:0 auto;cursor:move}</style></div></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Netty"><span class="toc-text">Netty</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E5%BC%95%E5%AF%BC"><span class="toc-text">1.引导</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-ByteBuf"><span class="toc-text">2.ByteBuf</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#ByteBuf-%E4%B8%BB%E8%A6%81%E5%88%86%E4%B8%BA%E4%B8%89%E7%A7%8D%E7%B1%BB%E5%9E%8B"><span class="toc-text">ByteBuf 主要分为三种类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1ByteBuf%E7%9A%84%E4%BD%BF%E7%94%A8%E6%A8%A1%E5%BC%8F"><span class="toc-text">2.1ByteBuf的使用模式</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E7%9B%B4%E6%8E%A5%E7%BC%93%E5%86%B2%E5%8C%BA"><span class="toc-text">2.直接缓冲区</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E5%A4%8D%E5%90%88%E7%BC%93%E5%86%B2%E5%8C%BA"><span class="toc-text">3.复合缓冲区</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2%E5%AD%97%E8%8A%82%E7%BA%A7%E6%93%8D%E4%BD%9C"><span class="toc-text">2.2字节级操作</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-2-1%E9%9A%8F%E6%9C%BA%E8%AF%BB%E5%86%99"><span class="toc-text">2.2.1随机读写</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-2-2%E4%B8%A4%E7%A7%8D%E7%B4%A2%E5%BC%95"><span class="toc-text">2.2.2两种索引</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-Channel"><span class="toc-text">3.Channel</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#ChannelHandler"><span class="toc-text">ChannelHandler</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#EventLoop"><span class="toc-text">EventLoop</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/03/08/something-about-crypto/" title="something about crypto"><img src="https://yili979.oss-cn-beijing.aliyuncs.com/img/image-20220124000713258.png" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="something about crypto"/></a><div class="content"><a class="title" href="/blog/2022/03/08/something-about-crypto/" title="something about crypto">something about crypto</a><time datetime="2022-03-08T12:01:13.000Z" title="发表于 2022-03-08 20:01:13">2022-03-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/03/08/golang%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86/" title="golang内存管理"><img src="https://yili979.oss-cn-beijing.aliyuncs.com/img/202203081958270.png" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="golang内存管理"/></a><div class="content"><a class="title" href="/blog/2022/03/08/golang%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86/" title="golang内存管理">golang内存管理</a><time datetime="2022-03-08T11:55:30.000Z" title="发表于 2022-03-08 19:55:30">2022-03-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/03/08/%E9%89%B4%E6%9D%83%E7%B3%BB%E7%BB%9F/" title="鉴权系统"><img src="https://yili979.oss-cn-beijing.aliyuncs.com/img/202203081950401.jpeg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="鉴权系统"/></a><div class="content"><a class="title" href="/blog/2022/03/08/%E9%89%B4%E6%9D%83%E7%B3%BB%E7%BB%9F/" title="鉴权系统">鉴权系统</a><time datetime="2022-03-08T11:49:53.000Z" title="发表于 2022-03-08 19:49:53">2022-03-08</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By yili</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>